package com.bracketbird.client.gui.club.tournament;


import com.bracketbird.client.*;
import com.bracketbird.client.gui.clientchannel.*;
import com.bracketbird.client.gui.club.tournament.rtc.*;
import com.bracketbird.client.model.keys.*;
import com.bracketbird.client.model.tournament.*;
import com.bracketbird.client.service.*;
import com.bracketbird.client.service.rtc.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
import com.bracketbird.clientcore.appcontrol.*;
import com.bracketbird.clientcore.gui.*;
import com.bracketbird.clientcore.model.keys.*;
import com.bracketbird.clientcore.service.*;
import com.bracketbird.clientcore.style.*;

/**
 *
 */
public class TeamsPage extends Page<TeamsPageController> {

    private VerticalComponent content;

    private LabelComponent header;
    private LabelComponent nextButton;


    private TournamentListener<TournamentStateChangedEvent> stateListener = new TournamentListener<TournamentStateChangedEvent>() {
        public void onChange(TournamentStateChangedEvent event) {

        }
    };
    private PopupPanel initPopup = new PopupPanel(true);
    private AddTeamButton addTeamButton;
    private DeleteTeamButton deleteTeamButton;
    private TeamsTable teamsTable;


    public TeamsPage() {
        super();
        content = new VerticalComponent();
        initWidget(content);
    }

    public void init() {
        RTC.getInstance().getTournament().addStateListener(stateListener);

        StyleIt.add(content, RTCLayoutFac2.CONTENT);
        //content.add(getHeader(), RTCLayoutFac.h1());
        //content.add(getParticipaters(), new TextLayout(null, "100%").paddingTop(30));


        content.add(getTeamsTable(), new TextLayout(null, "100%"));

        HorizontalComponent hc = new HorizontalComponent();

        HorizontalComponent buttons = new HorizontalComponent();
        buttons.add(getAddTeamButton(), new TextLayout(Vertical.MIDDLE));
        buttons.add(getDeleteTeamButton(), new TextLayout(0, 0, 0, 20, Vertical.MIDDLE));

        hc.add(buttons, new TextLayout(Horizontal.LEFT, Vertical.MIDDLE));
        hc.add(getNextButton(), new TextLayout(Horizontal.RIGHT, Vertical.MIDDLE));

        Button b = new Button("createToken");
        b.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                UserId userId = UserManager.getInstance().getUser().getId();
                TournamentChannelId channelId = RTC.getInstance().getTournament().getTournamentChannelId();
                BBService.createChannelToken(channelId, userId, new CallBack<CreateChannelTokenResult>() {
                    @Override
                    public void success(CreateChannelTokenResult result) {
                        System.out.println("Channel: token: " + result.getToken());
                        ClientChannel channel = new ClientChannel();
                        channel.openChannel(result.getToken());
                    }

                    @Override
                    public void fail(Throwable t) {
                        t.printStackTrace();
                    }
                });
            }
        });

        //content.add(b);

        content.add(hc, new TextLayout(40, 0, 0, 0, null, "100%"));
        //showInitPopoup();
    }

    private void showInitPopoup() {
        StyleIt.add(initPopup, new TextLayout().border(0));
        initPopup.add(new ImageComponent("addPlayerHelp.png"));
        initPopup.setPopupPosition(360, 200);
        initPopup.show();
    }

    public PopupPanel getInitPopup() {
        return initPopup;
    }

    public DeleteTeamButton getDeleteTeamButton() {
        if (deleteTeamButton == null) {
            deleteTeamButton = new DeleteTeamButton(getTeamsTable());
        }
        return deleteTeamButton;
    }


    public AddTeamButton getAddTeamButton() {
        if (addTeamButton == null) {
            addTeamButton = new AddTeamButton();
        }
        return addTeamButton;
    }


    public LabelComponent getNextButton() {
        if (nextButton == null) {
            nextButton = new LabelComponent("Continue >>");
            nextButton.setStyleName("colorbutton");
            nextButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    PageFlow.show(SettingsPageController.getInstance());
                }
            });
        }
        return nextButton;
    }

    public TeamsTable getTeamsTable() {
        if (teamsTable == null) {
            teamsTable = new TeamsTable();

        }
        return teamsTable;
    }


 

    public LabelComponent getHeader() {
        if (header == null) {
            header = new LabelComponent("Teams");
        }
        return header;
    }

    protected void setSubPageHolder(Page subPage) {
        //ignore
    }


    public void setFocus(boolean focus) {
        getTeamsTable().setFocus(focus);
    }

}